/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.process;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.form.client.Constants;
import com.genia.toolbox.web.gwt.form.client.CssNames;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.manager.FormManager;
import com.genia.toolbox.web.gwt.form.client.message.Messages;
import com.genia.toolbox.web.gwt.form.client.service.FormService;
import com.genia.toolbox.web.gwt.form.client.service.FormServiceAsync;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;

/**
 * Class containing static methods used in the module.
 */
public class ProcessForm
{
  /**
   * the static <code>FormServiceAsync</code> used by the application.
   */
  private static FormServiceAsync formService = null;



  /**
   * getter for the static <code>FormServiceAsync</code> used by the
   * application.
   * 
   * @return the static <code>FormServiceAsync</code> used by the application
   */
  public static FormServiceAsync getFormService()
  {
    synchronized (FormManager.class) {
      if (formService == null) {
        formService = (FormServiceAsync) GWT.create(FormService.class);
        final ServiceDefTarget endpoint = (ServiceDefTarget) formService;
        endpoint.setServiceEntryPoint(GwtHelper.getServiceName(Constants.MODULE_NAME, FormService.SERVICE_NAME));
      }
    }
    return formService;
  }



  /**
   * add a waiting message to a {@link Panel}. Warning: this method will clear
   * the panel.
   * 
   * @param panel
   *          the Panel to add the message to
   */
  public static void addWaitingMessage(final Panel panel)
  {
    Label loadingLabel = new Label("Waiting...");
    loadingLabel.addStyleName(CssNames.LOADING_LABEL_CSS_STYLE);
    GwtHelper.fillText(loadingLabel, Messages.LOADING_FORM_MESSAGE);
    panel.clear();
    panel.add(loadingLabel);
  }



  /**
   * retrieve a form from the server of the locale cache and use the
   * AsyncCallback to pass it back to the application.
   * 
   * @param formIdentifier
   *          the identifier of the form to retrieve
   * @param dataIdentifier
   *          the data identifier
   * @param callback
   *          the callback to pass the value back to the application
   */
  public static void getForm(final String formIdentifier, DataIdentifier dataIdentifier, AsyncCallback<Form> callback)
  {
    getFormService().getForm(formIdentifier, dataIdentifier, callback);
  }
}
